#include "spisok.h"
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>

struct tSList
{
Pointer data;
struct tSList* next;

};
SList *slist_preppend(SList *head, Pointer data)
{
        SList* t=(SList*)malloc(sizeof(SList));
        
        t->data=data;
        
        t->next=head;
        return t;
}
SList *slist_append(SList *head,Pointer data)
{
        SList* t=(SList*)malloc(sizeof(SList));
        t->data=data;
                
        t->next=NULL;
        if(head==NULL)
        {
                return t;
        }
        else
        {
                SList* ptr=head;

                while(ptr->next!=NULL)
                {
                        ptr=ptr->next;
                }
                ptr->next=t;

                return head;
        }
}

void slist_insert(SList *sibling, Pointer data)
{
        SList* t=(SList*)malloc(sizeof(SList));
        t->data=data;
        t->next=sibling->next;
        sibling->next=t;

}

SList *slist_find(SList *head, Pointer needle)
{
        SList * t=head;
        if(head==NULL)
        {
                return t;
        }
        
        else
        {
                while(t->data!=needle)
                {
                
                        t=t->next;
                }

                return t;
        }
}

SList *slist_remove(SList *head, Pointer data)
{
        SList* ptr;
        SList* t=head;
        if(head->data=data)
                {
                        ptr=t->next;
                        t=NULL;
                        free(t);
                }
                else
                {
        
        while(t->data!=data)
        {
                ptr=t;
                t=t->next;
        }
        ptr->next=t->next;
                t=NULL;
                free(t);
                }
           head=ptr;
        
        

        return head;


}
SList *slist_remove_all(SList *head, Pointer data)
{
        SList* ptr;
        SList*t=head;
        while(t->next!=NULL)
        {
               while(t->data!=data)
                {
                        ptr=t;
                        t=t->next;
                }
                ptr->next=t->next;
                                t=NULL;
                free(t);
                                t=ptr;

        }
         return head;
}
Pointer slist_remove_next(SList *sibling)
{
        SList* t;
        SList* ptr;
        t=sibling->next;
        ptr=t->next;
        sibling->next=ptr;
        free(t);
}

void slist_free(SList *head)
{
        SList* t=head;
        SList*ptr=head;
        while(t!=NULL)
        {
                ptr=t;
                t=t->next;
                free(ptr);
        }
}

unsigned slist_length(head)
{
        SList* t=head;
        int i=1;
        while(t->next!=NULL)
        {
                t=t->next;
                i++;
        }
        return i;
}
SList *slist_copy(SList *head)
{
        SList* head1=(SList*)malloc(sizeof(SList));
        SList* t=head;
        SList* ptr=head1;
        head1->next=NULL;
        
        
        
        
        ptr->data=t->data;
        while(t->next!=NULL)
        {
                t=t->next;
                slist_insert(ptr,t->data);
                
                ptr=ptr->next;
                
        }
        return head1;

}

SList *slist_reverse(SList *head)
{
        SList* t;
        SList* ptr;
        SList* new_head;
                new_head=head;
        t=new_head;
                ptr=t;
       
        
        
        while(ptr->next!=NULL)
        {
                
                t=ptr->next;
                                ptr->next=t->next;
                                t->next=new_head;
                                new_head=t;
                                
                                
              

        }
        return new_head;


}

SList *slist_concat(SList *head1, SList *head2)
{
        SList*t;
        t=head1;
        while(t->next!=NULL)
        {
                t=t->next;
        }
        t->next=head2;
        return head1;
}

int slist_foreach(SList *head,int (*func)(Pointer data, Pointer user_data), Pointer user_data)
{
        SList* t;
        SList* ptr;
        t=head;
        while(t!=NULL)
        {
                ptr=t->next;
				

                if (!func(t->data,user_data))
                {
					if(t->data==0)
					{
						free(t);
					}
                        return 0;
                }
                t=ptr;

                
                
        }
        
        return 1;
}
